import Dialog from "../../Game/dialog/Dialog";
import Scene from "../../Scene";
import { TButton, TLayout } from "../../SceneControl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class OilMineDialogScene extends Scene {

    btnsNode: cc.Node;

    protected onEnable(): void {
        super.onEnable();
        this.btnsNode = cc.find("bg/btns", this.node);
        this.btnsNode.children[0].on("active-in-hierarchy-changed", this.refreshBtns, this);
        this.btnsNode.children[1].on("active-in-hierarchy-changed", this.refreshBtns, this);
        this.initScene();
    }

    protected onDisable(): void {
        super.onDisable();
        this.btnsNode.children[0].targetOff(this);
        this.btnsNode.children[1].targetOff(this);
        this.btnsNode = null;
    }

    public initScene() {
        let layout = new TLayout();
        layout.buttonList = this.getBtns();
        this.tscene.addLayout(layout);
        this.tscene.onBack = () => {
            this.getComponent(Dialog).closeDialog();
        }
        this.tscene.joinController();
    }

    getBtns() {
        let ret = [[]];
        if (this.btnsNode.children[0].active) {
            let btn = new TButton(this.btnsNode.children[0]);
            btn.offset = cc.v3(90, 0)
            ret[0].push(btn);
        }

        if (this.btnsNode.children[1].active) {
            let btn = new TButton(this.btnsNode.children[1]);
            btn.offset = cc.v3(90, 0)
            ret[0].push(btn);
        }
        return ret;
    }

    refreshBtns() {
        this.tscene.currLayout.buttonList = this.getBtns();
        this.tscene.apply();
    }
}
